
/*
 * FaceMovie adds some needed functionality to simplify the faceplayer program
 */
class FaceMovie extends JMCMovie {
  int LMTBufferSize = 5;
  float[] lastMovieTimes = new float[LMTBufferSize];
  int movieNum;
  int faceScaleFactor;
  
  // Existing 
  FaceMovie(processing.core.PApplet parent, java.io.File file) {
    super(parent, file);
  }
  FaceMovie(processing.core.PApplet parent, java.lang.String filename) {
    super(parent, filename);
  }
  FaceMovie(processing.core.PApplet parent, java.net.URI uri) {
    super(parent, uri);
  }
  FaceMovie(processing.core.PApplet parent, java.net.URL url) {
    super(parent, url);
  }
  
  boolean isDone() {
    // Note: There are some weird states that the time() method gets into
    //   1. it can be -Infinity. we check for that
    //   2. time doesn't always hit the duration exactly. the negative offset 
    //      seems to be necessary to keep the movie from playing again.
    return currMovie.time() >= (currMovie.duration() - .05);
  }
  
  // NOTE: This method depends on calls
  boolean isFrozen() {
    boolean allSame = true;
    boolean allZero = true;
    // if all array entries are the same, we're frozen
    for (int i = 0; i < lastMovieTimes.length; i++) {
      if (lastMovieTimes[i] != lastMovieTimes[0]) allSame = false;
      if (lastMovieTimes[i] != 0) allZero = false;
    }
    // shift everything in the array
    for (int i = lastMovieTimes.length-1; i > 0; i--) {
      lastMovieTimes[i] = lastMovieTimes[i-1];
    }
    lastMovieTimes[0] = this.time();
    //println(allSame + " | " + allZero);
    return (allSame && !allZero);
  }
  
  void load(int movieNumber) {
    this.stop();
    this.movieNum = movieNumber;
    String movieFileName = (String) fileNames.get(movieNumber);
    this.faceScaleFactor = 
      faceScales.containsKey(movieNumber) ? (Integer) faceScales.get(movieNumber) : 0;
    // -1 means you're in a faceloop
    if (movieNumber < 0) {
      movieFileName = (String) faceloopClips.get(int(random(faceloopClips.size())));
    }
    this.switchVideo("movies/" + movieFileName);
    this.play();
  }
  
  int getNum() {
    return movieNum;
  }
  
  float getFaceScale() {
    return .98 + (this.faceScaleFactor/float(100));
  }
}